package com.zzw.aspect;

import com.zzw.bean.ExportTask;
import com.zzw.bean.TChargeBox;
import com.zzw.dao.ExportTaskMapper;
import com.zzw.service.TChargeBoxService;
import com.zzw.utils.ExcelUtil;
import com.zzw.utils.RedisUtil;
import com.zzw.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executors;

/**
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class AsyncService {
    @Autowired
    private TChargeBoxService tChargeBoxService;
    @Autowired
    private ExportTaskMapper taskMapper;

    @Async
    public void processData(HttpServletResponse response) throws IOException {
        try {
            // 模拟长时间任务
            Thread.sleep(5000);  // 假设这个操作需要5秒
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 处理完毕后写入响应
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("<h1>线程: 你好</h1>");
        writer.flush();
        writer.close();
    }

    @Async
    public void excel(TChargeBox tChargeBox,HttpServletRequest request,HttpServletResponse response) {
        try {
            // 模拟长时间任务
            Thread.sleep(5000);  // 假设这个操作需要5秒
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 获取数据
        List<TChargeBox> tChargeBoxes = tChargeBoxService.selectTchargeBoxList(tChargeBox);

        // 定义表头
        String[] headers = {"ID", "文件名称", "创建时间", "下载次数"};

        // 定义表头与字段的映射关系
        LinkedHashMap<String, String> headerFieldMap = new LinkedHashMap<>();
        headerFieldMap.put("id", "ID");
        headerFieldMap.put("rejectReason", "驳回原因");
        headerFieldMap.put("createTime", "创建时间");

        // 调用工具类进行导出
        try {
            ExcelUtil.exportExcel(request,response, headerFieldMap, tChargeBoxes,
                    "充电宝机柜管理.xlsx", "充电宝机柜管理");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String createTask(TChargeBox tChargeBox, HttpServletResponse response) {
        String taskId = UUID.randomUUID().toString();
        ExportTask exportTask = new ExportTask(taskId, (byte) 0, null, new Date(), null);
        taskMapper.insert(exportTask);

        HttpServletRequest request = WebUtils.servletRequest.get();
        Executors.newSingleThreadExecutor().submit(new Runnable() {
            @Override
            public void run() {
                updateTaskStatus(taskId, (byte) 1, null);
                excel(tChargeBox, request, response);
                updateTaskStatus(taskId, (byte) 2, "");
            }
        });
        return "";
    }

    private void updateTaskStatus(String taskId, byte status, String filePath) {
        ExportTask task = taskMapper.selectByPrimaryKey(taskId);
        task.setStatus(status);
        task.setFilePath(filePath);
        task.setFinishTime(new Date());
        taskMapper.updateByPrimaryKeySelective(task);
    }

    public void myExcel(TChargeBox tChargeBox, HttpServletResponse response) {

    }
}
